package com.dingup.api.base;

import com.dingup.api.enums.ParamTypeEnum;
import com.suyeer.basic.util.BLogUtil;
import org.apache.commons.lang.StringUtils;
import org.apache.http.HttpStatus;

import static org.apache.commons.lang.StringUtils.EMPTY;

/**
 * Created by jun on 2018/7/13.
 */
public class BaseUtil {
    /**
     * User: jun
     * Date: 2017/8/8
     * Desc: 记录错误信息,并写入到保存到页面返回中
     */
    public static <T> void saveErrorMsgAndData(HttpResContent<T> hrc, Exception e) {
        saveErrorMsgAndData(hrc, HttpStatus.SC_NOT_IMPLEMENTED, e);
    }

    public static <T> void saveErrorMsgAndData(HttpResContent<T> hrc, int errorCode, Exception e) {
        saveErrorMsgAndData(hrc, errorCode, null, e);
    }
    public static <T> void saveSimpleMsgAndData(HttpResContent<T> hrc, int errorCode, Exception e) {
        saveSimpleMsgAndData(hrc, errorCode, null, e);
    }
    public static <T> void saveErrorMsgAndData(HttpResContent<T> hrc, int errorCode, T data, Exception e) {
        String errMsg = String.format("%s:FAIL, %s", hrc.getFuncDes(), e.getMessage());
        BLogUtil.error(errMsg, e);
        if (e.getMessage() != null && e.getMessage().indexOf("###") > 0) {
            errMsg = "内部异常!";
        }
        hrc.fillData(errorCode, errMsg, data);
    }
    public static <T> void saveSimpleMsgAndData(HttpResContent<T> hrc, int errorCode, T data, Exception e) {
        String errMsg = e.getMessage();
        BLogUtil.error(errMsg, e);
        hrc.fillData(errorCode, errMsg, data);
    }

    /**
     * User: jun
     * Date: 2017/8/8
     * Desc: 写入操作成功信息
     */
    public static <T> void saveSuccessMsgAndData(HttpResContent<T> hrc, T data) {
        String info = String.format("%s:Success!", hrc.getFuncDes());
        BLogUtil.info(info);
        hrc.fillData(HttpStatus.SC_OK, info, data);
    }

    /**
     * User: jun
     * Date: 2017/8/8
     * Desc: check param
     */
    public static void checkParam(String pName, Object pVal) throws IllegalArgumentException {
        checkParam(ParamTypeEnum.STRING_NOT_BLANK, pName, pVal, true);
    }

    public static void checkParam(String pName, Object pVal, Boolean ifCheckJsInjectionAttack) throws IllegalArgumentException {
        checkParam(ParamTypeEnum.STRING_NOT_BLANK, pName, pVal, ifCheckJsInjectionAttack);
    }

    public static void checkParam(ParamTypeEnum pType, String pName, Object pVal) throws IllegalArgumentException {
        checkParam(pType, pName, pVal, true);
    }

    /**
     * User: jun
     * Date: 2017/8/8
     * Desc: check param
     */
    public static void checkParam(ParamTypeEnum pType, String pName, Object pVal, Boolean ifCheckJsInjectionAttack) throws IllegalArgumentException {
        try {
            String value = pVal.toString();
            if (ifCheckJsInjectionAttack) {
                if (value.toLowerCase().replaceAll("\\s", EMPTY).contains("</script>")) {
                    throw new SecurityException();
                }
            }
            Boolean flag = false;
            switch (pType) {
                case STRING_NOT_BLANK:
                    flag = StringUtils.isBlank(value);
                    break;
                case STRING_LENGTH_GTE_6:
                    flag = value.trim().length() < 6;
                    break;
                case STRING_LENGTH_LTE_20:
                    flag = value.trim().length() > 20;
                    break;
                case INTEGER:
                    Integer.valueOf(value);
                    break;
                case INTEGER_GT_0:
                    flag = Integer.parseInt(value) <= 0;
                    break;
                case LONG_GT_0:
                    flag = Long.parseLong(value) <= 0;
                    break;
                default:
                    break;
            }
            if (flag) {
                throw new Exception();
            }
        } catch (NullPointerException e) {
            throw new IllegalArgumentException(String.format("参数 %s 不能为 null", pName));
        } catch (SecurityException e) {
            throw new IllegalArgumentException(String.format("尝试JS注入攻击的参数 %s = %s", pName, pVal.toString()));
        } catch (Exception e) {
            throw new IllegalArgumentException(String.format("参数 %s = %s 不合法: %s", pName, pVal.toString(), pType.getDes()));
        }
    }
}
